package com.qiangqiang.task.server.service;

import com.qiangqiang.task.server.consts.TimeConst;
import com.qiangqiang.task.server.dto.*;
import com.qiangqiang.task.server.entity.*;
import com.qiangqiang.task.server.mapper.PostMapper;
import com.qiangqiang.task.server.mapper.UserMapper;
import com.qiangqiang.task.server.util.TimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author : sky
 * @date : 2018/7/17
 * @description :
 */
public class BaseService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MomentsCommentsService momentsCommentsService;

    @Autowired
    private PostMapper postMapper;

    public TaskDTO beanToDTO(Task task) {
        TaskDTO taskDTO = new TaskDTO();
        BeanUtils.copyProperties(task, taskDTO);
        taskDTO.setPublicTime(TimeFormat.converTime(task.getCreateTime()));
        taskDTO.setEndTime(TimeFormat.converTime(task.getEndTime()));
        UserInfo userInfo = userMapper.queryUserById(task.getUserId());
        taskDTO.setPhone(userInfo.getPhone());
        taskDTO.setCampus(userInfo.getCampus());
        return taskDTO;
    }

    public PostDTO beanToDTO(Post post) {
        PostDTO postDTO = new PostDTO();
        BeanUtils.copyProperties(post, postDTO);
        UserInfo userInfo = userMapper.queryUserById(post.getPublisherId());
        postDTO.setPublishName(userInfo.getNickName());
        postDTO.setAchievement(userInfo.getAchievement());
        postDTO.setUpdateTime(TimeFormat.converTime(post.getUpdateTime()));
        postDTO.setLabel(userInfo.getLabel());
        return postDTO;
    }

    public CommentsDTO beanToDTO(Comments comments) {
        CommentsDTO commentsDTO = new CommentsDTO();
        BeanUtils.copyProperties(comments, commentsDTO);
        UserInfo userInfo = userMapper.queryUserById(comments.getFromId());
        commentsDTO.setFromName(userInfo.getNickName());
        commentsDTO.setAchievement(userInfo.getAchievement());
        commentsDTO.setLabel(userInfo.getLabel());
        commentsDTO.setUpdateTime(TimeFormat.converTime(comments.getUpdateTime()));
        return commentsDTO;
    }

    public FollowDTO beanToDTO(long userId) {
        FollowDTO followDTO = new FollowDTO();
        UserInfo userInfo = userMapper.queryUserById(userId);
        followDTO.setUserName(userInfo.getNickName());
        followDTO.setAchievement(userInfo.getAchievement());
        return followDTO;
    }

    public MomentsDTO beanToDTO(Moments moments) {
        MomentsDTO momentsDTO = new MomentsDTO();
        BeanUtils.copyProperties(moments, momentsDTO);
        momentsDTO.setUserName(userMapper.queryNickNameByUserId(moments.getUserId()));

        List<MomentsCommentsDTO> commentsList = momentsCommentsService.findAllCommentsByMomentsId(moments.getMomentsId());
        momentsDTO.setCommentsDTOList(commentsList);
        return momentsDTO;
    }

    public MomentsCommentsDTO beanToDTO(MomentsComments momentsComments) {
        MomentsCommentsDTO momentsCommentsDTO = new MomentsCommentsDTO();
        BeanUtils.copyProperties(momentsComments, momentsCommentsDTO);
        momentsCommentsDTO.setFromName(userMapper.queryNickNameByUserId(momentsComments.getUserId()));
        return momentsCommentsDTO;
    }

    public MessageDTO beanToDTO(Message message) {
        PostDTO postDTO = beanToDTO(postMapper.queryPostByPostId(message.getCommonId()));
        String name = userMapper.queryNickNameByUserId(message.getFromId());
        String time = TimeFormat.converTime(message.getCreateTime());
        MessageDTO dto = new MessageDTO(name, time, postDTO);
        dto.setCommonId(String.valueOf(message.getCommonId()));
        dto.setType(message.getType());
        return dto;
    }

}
